//
//  ReleaseStep5ViewController.m
//  Gunz
//
//  Created by Joggy on 16/4/16.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "ReleaseStep5ViewController.h"
#import "BarButtonView.h"
#import "ReleaseStep6ViewController.h"
#import "GZNetRequest.h"
#import "QYNotice.h"
#import "CoreStatus.h"

@interface ReleaseStep5ViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation ReleaseStep5ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    rowHeight = @[
                  @[@"47", @"47"],
                  @[@"47", @"47", @"100"]
                  ];
    rowTitle = @[
                 @[@"您需支付押金:", @"使用智币个数:"],
                 @[@"您需支付:", @"选择支付方式:", @""]
                 ];
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(endEdit)]];
    NSLog(@"%@", _data);
}

- (void)prepareForNav {
    [super prepareForNav];
    self.title = @"支付";
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
}

- (void)prepareForLayout {
    [super prepareForLayout];
    //底部按钮
    BarButtonView *barButton = [[BarButtonView alloc] initWithFrame:CGRectMake(0, [GZConst getScreenHeight] - 64 - BBViewHeight, [GZConst getScreenWidth], BBViewHeight) name:@"下一步"];
    [barButton.but addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:barButton];
    //标题lab
    UILabel *titleConfig = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 17)];
    titleConfig.layer.position = CGPointMake([GZConst getScreenWidth]/2, 34);
    titleConfig.textColor = CUSTOM_GREEN;
    titleConfig.font = [UIFont systemFontOfSize:14];
    titleConfig.textAlignment = NSTextAlignmentCenter;
    titleConfig.text = @"***项目";
    [self.view addSubview:titleConfig];
    //
    UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(0, 56, screenWidth, screenHeight - 51 - 64 - 56)];
    table.separatorColor = TEXT_BORDER_COLOR;
//    table.backgroundColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:251/255.0 alpha:1];
    table.backgroundColor = [UIColor clearColor];
    [table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"zfCell"];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
}

#pragma 事件方法

- (void)endEdit {
    [self.view endEditing:true];
}

- (void)back {
    [self.navigationController popViewControllerAnimated:true];
}

- (void)nextStep {
    [QYNotice showProcess:@"正在发布中" toView:self.view];
    if([CoreStatus isNetworkEnable]) {
        [[GZNetRequest getPOSTManager] POST:@"http://180.209.64.49:8001/task/uploadTask" parameters:_data progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@", responseObject);
            [QYNotice dismissWithDelay:0];
            NSDictionary *dic = responseObject;
            NSString *status = dic[@"status"];
            if([status isEqualToString:@"sucess"]) {
                ReleaseStep6ViewController *next = [[ReleaseStep6ViewController alloc] init];
                [self.navigationController pushViewController:next animated:true];
            }
            else {
                [QYNotice showError:@"发布失败,请检查任务信息" toView:self.view];
                [QYNotice dismissWithDelay:3];
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"%@", error);
            [QYNotice showError:@"请求失败" toView:self.view];
            [QYNotice dismissWithDelay:0];
        }];
    }
    else {
        [QYNotice showWarning:@"未检测到网络" toView:self.view];
        [QYNotice dismissWithDelay:3];
    }
}

- (void)paySelected:(UIButton*)button {
    if(pay.zhifubao != button) {
        pay.zhifubao.selected = false;
    }
    if(pay.weixin != button) {
        pay.weixin.selected = false;
    }
    if(pay.unionPay != button) {
        pay.unionPay.selected = false;
    }
    button.selected = !button.selected;
}

#pragma 代理方法

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *te = rowHeight[section];
    return te.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 12;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 12)];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zfCell"];
    cell.backgroundColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:251/255.0 alpha:1];
    NSArray *tem = rowHeight[indexPath.section];
    tableView.rowHeight = [tem[indexPath.row] intValue];
    NSArray *temTitle = rowTitle[indexPath.section];
    cell.textLabel.text = temTitle[indexPath.row];
    cell.textLabel.textColor = TEXT_DETAIL_COLOR;
    if(indexPath.section == 0) {
        if(indexPath.row == 0) {
            UILabel *depositLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 63*proportion, 19)];
            depositLab.layer.position = CGPointMake(160*proportion, 24);
            depositLab.textColor = [UIColor colorWithRed:1 green:125/255.0 blue:2/255.0 alpha:1];
            depositLab.font = [UIFont systemFontOfSize:19];
            depositLab.textAlignment = NSTextAlignmentRight;
            depositLab.text = @"50¥";
            [cell.contentView addSubview:depositLab];
        }
        else {
            UITextField *textfie = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 63*proportion, 36)];
            textfie.layer.position = CGPointMake(160*proportion, 24);
            textfie.layer.borderWidth = 1;
            textfie.layer.borderColor = TEXT_BORDER_COLOR.CGColor;
            textfie.layer.cornerRadius = 2;
            textfie.backgroundColor = [UIColor whiteColor];
            textfie.textAlignment = NSTextAlignmentRight;
            textfie.textColor = TEXT_DETAIL_COLOR;
            textfie.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 36)];
            textfie.rightViewMode = UITextFieldViewModeAlways;
            textfie.text = @"500";
            [cell.contentView addSubview:textfie];
            UILabel *coinBalance = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 14)];
            coinBalance.layer.position = CGPointMake(screenWidth - 70, 24);
            coinBalance.textColor = [UIColor colorWithRed:1 green:198/255.0 blue:0 alpha:1];
            coinBalance.font = [UIFont systemFontOfSize:12];
            coinBalance.textAlignment = NSTextAlignmentRight;
            coinBalance.text = @"您的智币共计500个";
            [cell.contentView addSubview:coinBalance];
        }
    }
    else {
        if(indexPath.row == 0) {
            UILabel *shouldPay = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 63*proportion, 19)];
            shouldPay.layer.position = CGPointMake(160*proportion, 24);
            shouldPay.textColor = [UIColor colorWithRed:1 green:125/255.0 blue:2/255.0 alpha:1];
            shouldPay.font = [UIFont systemFontOfSize:19];
            shouldPay.textAlignment = NSTextAlignmentRight;
            shouldPay.text = @"45¥";
            [cell.contentView addSubview:shouldPay];
        }
        else if(indexPath.row == 2) {
            pay = [[Step5Pay alloc] init];
            [pay.zhifubao addTarget:self action:@selector(paySelected:) forControlEvents:UIControlEventTouchUpInside];
            [pay.weixin addTarget:self action:@selector(paySelected:) forControlEvents:UIControlEventTouchUpInside];
            [pay.unionPay addTarget:self action:@selector(paySelected:) forControlEvents:UIControlEventTouchUpInside];
            [cell.contentView addSubview:pay];
        }
    }
    return cell;
}

@end
